//
//  MainTabBar.swift
//  SwiftDemo
//
//  Created by 李文强 on 2017/1/11.
//  Copyright © 2017年 李文强. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    //写按钮
    lazy var composeButton:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:""), for: UIControlState.normal)
        btn.setImage(UIImage(named:""), for: UIControlState.selected)
        btn.backgroundColor = UIColor.orange
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 3
        btn.titleLabel?.text = "+"
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
        self.addSubview(btn)
        return btn
    }()
//    lazy var firstButton:UIButton = {
//        let btn = UIButton()
////        btn.setImage(UIImage(named:""), for: UIControlState.normal)
////        btn.setImage(UIImage(named:""), for: UIControlState.selected)
//        btn.backgroundColor = UIColor.clear
//        btn.titleLabel?.text = "First"
//        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
//        self.addSubview(btn)
//        return btn
//    }()
//
//    lazy var secondButton:UIButton = {
//        let btn = UIButton()
////        btn.setImage(UIImage(named:""), for: UIControlState.normal)
////        btn.setImage(UIImage(named:""), for: UIControlState.selected)
//        btn.backgroundColor = UIColor.clear
//        btn.titleLabel?.text = "Second"
//        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
//        self.addSubview(btn)
//        return btn
//    }()
//
//    lazy var thirdButton:UIButton = {
//        let btn = UIButton()
////        btn.setImage(UIImage(named:""), for: UIControlState.normal)
////        btn.setImage(UIImage(named:""), for: UIControlState.selected)
//        btn.backgroundColor = UIColor.clear
//        btn.titleLabel?.text = "Third"
//        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
//        self.addSubview(btn)
//        return btn
//    }()
//
//    lazy var forthButton:UIButton = {
//        let btn = UIButton()
////        btn.setImage(UIImage(named:""), for: UIControlState.normal)
////        btn.setImage(UIImage(named:""), for: UIControlState.selected)
//        btn.backgroundColor = UIColor.clear
//        btn.titleLabel?.text = "Forth"
//        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
//        self.addSubview(btn)
//        return btn
//    }()


    private let buttonCount = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        /// 设置每个按钮的宽高和基准尺寸
        let w = self.bounds.width / CGFloat(buttonCount)
        let h = self.bounds.height-10
        let frame = CGRect(x:0,y:5,width:40,height:h)
        
        var index: CGFloat = 0
        for view in self.subviews {
            if (view is UIControl && !(view is UIButton)) {
                /**
                 *  当前控件是响应者对象，并且不是 UIButton 类型时参照设置尺寸位置
                 */
                view.frame = frame
                view.frame = view.frame.offsetBy(dx: index * w, dy: 0)
                index += (index == 1) ? 2 : 1
            }else
            {
                /**
                 *  设置撰写按钮的尺寸和位置
                 */
                self.composeButton.frame = frame

                self.composeButton.center = CGPoint.init(x:self.bounds.width * 0.5, y:self.bounds.height * 0.5 )

            }
        }
    }
}
